package com.api.shopapi.mapper;

import com.api.shopapi.entity.Building;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface BuildingMapper {

    @Insert("insert into building " +
            "(BuildingCode,Name,Floor,StaffCode,InstitutionCode) " +
            "value(#{building.BuildingCode},#{building.Name},#{building.Floor}," +
            "#{building.StaffCode},#{building.InstitutionCode})")
    int insert(@Param("building") Building building);

    @Delete("delete from building where BuildingCode=#{buildingCode}")
    int deleteByBuildingCode(@Param("buildingCode") String buildingCode);

    @Update("update building set Name=#{building.Name},Floor=#{building.Floor}," +
            "StaffCode=#{building.StaffCode},InstitutionCode=#{building.InstitutionCode} " +
            "where BuildingCode=#{building.BuildingCode}")
    int updateByBuildingCode(@Param("building") Building building);

    @Select("select * from building")
    List<Building> getListAll();

    @Select("select * from building where InstitutionCode = #{institutionCode}")
    List<Building> getListByStaffCode(@Param("institutionCode") String institutionCode);

    @Select("<script>" +
            "select * from building " +
            "where InstitutionCode = #{institutionCode} " +
            "<if test ='name !=null'>" +
            "and Name like concat('%',#{name},'%') " +
            "</if>" +
            "</script>")
    List<Building> getPageList(@Param("institutionCode") String institutionCode,@Param("name") String name);

    @Select("select * from building where buildingCode=#{buildingCode} limit 1")
    Building getByBuildingCode(@Param("buildingCode") String buildingCode);

    @Select("select * from building where InstitutionCode = #{institutionCode} and Name=#{name} limit 1")
    Building getByBuildingName(@Param("name") String name,@Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select * from building " +
            "where InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode}) " +
            "</script>")
    List<Building> listByGovCode(@Param("governmentCode") String governmentCode);


}
